#include "MenuView.h"
#include "ViewContent.h"

#include "../Kernel.h"

using namespace Views;

MenuView::MenuView(const ViewContent &content, TCODConsole *parent)
	: IView(content.getName()), parent(parent)
{
	init(content);
}

MenuView::~MenuView()
{
}

void MenuView::init(const ViewContent &content)
{
	assert(content.getType() == VIEW_MENU);

	viewport.x = content.getWidth();
	viewport.y = content.getHeight();
	position.x = content.getX();
	position.y = content.getY();
	alpha.x = content.getForegroundAlpha();
	alpha.y = content.getBackgroundAlpha();
	alignment = content.getAlignment();

	console = std::unique_ptr<TCODConsole>(new TCODConsole(viewport.x, viewport.y));
	console->setAlignment((TCOD_alignment_t)alignment);
	console->setDefaultBackground(TCODColor::black);
	console->setDefaultForeground(TCODColor::white);
	console->clear();

	if(!content.getBackgroundImage().empty())
		background_img = std::unique_ptr<TCODImage>(new TCODImage((Kernel::getSingleton()->getResourceDir()+"Backgrounds/"+content.getBackgroundImage()).c_str()));

	logo_img = std::unique_ptr<TCODImage>(new TCODImage((Kernel::getSingleton()->getResourceDir()+"Backgrounds/"+"logo.png").c_str()));
}

void MenuView::update(float dt)
{
}

void MenuView::render()
{
	//if(background_img) background_img->blit(console.get(), 45.0f, 30.0f, TCOD_BKGND_SET, 0.5f, 0.5f, 0.0f);
	if(background_img) background_img->blit2x(console.get(), 0,0);
	//if(logo_img) logo_img->blit2x(console.get(), 0,0,0,0,-1,-1,TCOD_BKGND_MULTIPLY);
	TCODConsole::blit(console.get(), 0, 0, viewport.x, viewport.y, parent, position.x, position.y, alpha.x, alpha.y);
}

bool MenuView::inputKeyDown(TCOD_key_t &key, TCOD_mouse_t &mouse)
{
	if(key.vk == TCODK_ESCAPE)
		Kernel::getSingleton()->exit();

	return false;
}

bool MenuView::inputKeyUp(TCOD_key_t &key, TCOD_mouse_t &mouse)
{
	return false;
}

bool MenuView::motion(TCOD_key_t &key, TCOD_mouse_t &mouse)
{
	return false;
}

bool MenuView::mousePressed(TCOD_key_t &key, TCOD_mouse_t &mouse)
{
	return false;
}

bool MenuView::mouseReleased(TCOD_key_t &key, TCOD_mouse_t &mouse)
{
	return false;
}
